using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Microsoft.SharePoint.Export.ExchangePublicFolders
{
	/// <summary>
	/// Summary description for frmExtensions.
	/// </summary>
	public class ExtensionsFrame : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox lstValues;
		/// <summary>
		/// This is the list of the selected attachmentextensions
		/// </summary>
		public System.Windows.Forms.ListBox lstSelected;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnRemove;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.TextBox txtExtension;
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		/// <summary>
		/// This is the main constructor
		/// </summary>

		public ExtensionsFrame()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lstValues = new System.Windows.Forms.ListBox();
			this.lstSelected = new System.Windows.Forms.ListBox();
			this.btnAdd = new System.Windows.Forms.Button();
			this.btnRemove = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.txtExtension = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// lstValues
			// 
			this.lstValues.Location = new System.Drawing.Point(16, 40);
			this.lstValues.MultiColumn = true;
			this.lstValues.Name = "lstValues";
			this.lstValues.Size = new System.Drawing.Size(120, 82);
			this.lstValues.TabIndex = 0;
			// 
			// lstSelected
			// 
			this.lstSelected.Location = new System.Drawing.Point(208, 8);
			this.lstSelected.Name = "lstSelected";
			this.lstSelected.Size = new System.Drawing.Size(120, 121);
			this.lstSelected.TabIndex = 1;
			// 
			// btnAdd
			// 
			this.btnAdd.Location = new System.Drawing.Point(152, 16);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(40, 23);
			this.btnAdd.TabIndex = 2;
			this.btnAdd.Text = "-->";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// btnRemove
			// 
			this.btnRemove.Location = new System.Drawing.Point(152, 56);
			this.btnRemove.Name = "btnRemove";
			this.btnRemove.Size = new System.Drawing.Size(40, 23);
			this.btnRemove.TabIndex = 3;
			this.btnRemove.Text = "<--";
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(168, 144);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 4;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(256, 144);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "button2";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// txtExtension
			// 
			this.txtExtension.Location = new System.Drawing.Point(16, 8);
			this.txtExtension.Name = "txtExtension";
			this.txtExtension.Size = new System.Drawing.Size(120, 20);
			this.txtExtension.TabIndex = 6;
			this.txtExtension.Text = "";
			// 
			// ExtensionsFrame
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(344, 182);
			this.Controls.Add(this.txtExtension);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnRemove);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.lstSelected);
			this.Controls.Add(this.lstValues);
			this.Name = "ExtensionsFrame";
			this.Text = "frmExtensions";
			this.Load += new System.EventHandler(this.ExtensionsFrame_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			if (txtExtension.Text.Length>0)
			{
				lstSelected.Items.Add((object)txtExtension.Text.ToString());
				lstValues.ClearSelected();
				txtExtension.Text = "";
			}
			else
			{
				foreach(object itemChecked in lstValues.SelectedItems)
				{	
					lstSelected.Items.Add((object)itemChecked.ToString());
				}
				lstValues.ClearSelected();
			}
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			//getExtensions();
			this.Hide();
		}

		private void ExtensionsFrame_Load(object sender, System.EventArgs e)
		{
			XmlNodeList nodel= getExtensions();;
			
			foreach (XmlNode nd in nodel)
				lstValues.Items.Add ((object)nd.InnerText);
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Hide();
		}

		private XmlNodeList getExtensions()
		{
			XmlDocument doc = new XmlDocument();
			doc.Load(Application.StartupPath + @"..\..\..\config.xml");
			XmlNodeList nodeList = doc.SelectNodes("//configuration/extensions/extension");
			return (nodeList);
		}
	}
}
